//oj链接：https://leetcode.cn/problems/lru-cache/submissions/
class LRUCache {
public:
    LRUCache(int capacity) {
        cap = capacity;
    }
    
    int get(int key) {
        auto it = um.find(key);
        int res = -1;
        if(it != um.end())
        {
            res = it->second->second;
            lt.splice(lt.begin(),lt,it->second);
        }
        return res;
    }
    
    void put(int key, int value) {
        auto it = um.find(key);
        if(it == um.end())
        {
            if(cap == um.size())
            {
                pair<int,int> back = lt.back();
                um.erase(back.first);
                lt.pop_back();
            }
            lt.push_front(make_pair(key,value));
            um[key] = lt.begin();
        }
        else  // 修改值
        {
            it->second->second = value;
            lt.splice(lt.begin(),lt,it->second);
        }
    }

    typedef list<pair<int,int>>::iterator LTIterator;
private:
    int cap;
    list<pair<int,int>> lt;
    unordered_map<int,LTIterator> um;
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */
